How to run docker for development environments

Ah, Docker, the unsung hero of DevOps! It’s like the Swiss Army knife of the tech world, allowing you to run your applications with ease, whether you’re at your local coffee shop or floating in the digital clouds. And let’s face it, if Docker were a superhero, it would be the “Container Crusader,” saving us from the chaos of compatibility issues and deployment woes!

In this post you will find some base Docker images and development environments you can use as a base for you own projects.

All the files are stored in this Github repository.

PHP Docker Image

Here you can find base docker images for both PHP 7 and PHP 8.

Let’s take a look at how to build a PHP 8 image.

Beginning with the Dockerfile:

FROM php:8.2-fpm

# xdebug.ini file path
ARG xdebug_conf_file="/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini"

# Your application root path
ARG app_dir="/app"

RUN apt-get update \
    && apt-get install -y \
        libpq-dev \
        libgmp-dev \
        libicu-dev \
    && docker-php-ext-configure intl \
    && docker-php-ext-install \
        bcmath \
        intl \
        opcache \
        pcntl \
        pdo \
        pdo_pgsql \
        pgsql \
    && docker-php-ext-enable \
        bcmath \
        intl \
        opcache \
        pcntl \
        pdo \
    && pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
    && rm -rf /var/lib/apt/lists/* /tmp/*

# Some suggested debug packages and other useful stuff
# RUN apt-get update \
#     && apt-get install -y \
#         ca-certificates \
#         curl \
#         wget \
#         git \
#         vim \
#         less \
#         iputils-ping \
#         inetutils-telnet \
#     && rm -rf /var/lib/apt/lists/* /tmp/*



# Expose ports 9000 (php-fpm) and 9003 (xdebug)
EXPOSE 9000 9003

COPY ./php.ini "$PHP_INI_DIR/php.ini"
COPY ./xdebug.ini ${xdebug_conf_file}

WORKDIR ${app_dir}

# Copy your application files/folders to the container
# COPY ./app ./app
# COPY ./composer.json ./
# COPY ./composer.lock ./
# COPY ...
# COPY ...

COPY index.php index.php
COPY docker-cmd.sh docker-cmd.sh

# Run composer install
# RUN XDEBUG_MODE=on composer install --no-interaction --prefer-dist

CMD ./docker-cmd.sh

The above Dockerfile gets the official PHP 8.2 image and add some extra stuff.

We run apt-get, pecl and some other command for installing dependencies and libraries.

We COPY the php.ini and xdebug.ini, from our repository, into the docker image.

Also we COPY our application files to the image, so it has all the resources need to run the application.

Finally we set the CMD command to run the docker-cmd.sh. This shell script is responsible for executing some PHP related commands and, after that, keeping php-fpm running.

Cloning the repository and running the images

You can clone my Github repository, build and run all of my docker image examples.

Each directory has its own README, where you can find the commands to build and run the images and containers.

Related posts

Leave a Comment